// pages/signin/signin.js
let app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    classNumber: 0,
    running: false,
    signined: false,
    show: true,
    latitude: 23.099994,
    longitude: 113.324520,
    signinId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      classNumber: options.classNumber
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function() {

  //   // 查询当前课程是否正在签到
  //   let running = true;
  //   this.setData({
  //     running: running
  //   })

    
  // },




  onShow: function() {


    // 获取当前班级的签到开启情况，是否存在签到，是否开启，用户是否已经签到过

    // 1. 获取是否存在签到

    wx.cloud.callFunction({
      name: "isExistSignin",
      data: {
        classNumber: this.data.classNumber,
      }
    }).then(res => {
      console.log(res)

      if (res.result.running) {
        this.setData({
          running: true,
          signinId: res.result.sId
        })
        // 2. 用户是否签到过
        wx.cloud.callFunction({
          name: "isSignined",
          data: {
            signinId: this.data.signinId,
            userId: app.globalData.userInfo._id
          }
        }).then(res => {
          console.log(res)
          if (!res.result) {
            this.setData({
              signined: false
            })
          } else {
            this.setData({
              signined: true
            })
          }
        }).catch(res => {
          console.log(res)
        })

      } else {
        this.setData({
          running: false
        })
      }
    }).catch(res => {
      console.log(res)
    })

    
  },


  map: function() {
    let that = this
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.detail.markerId)
  },
  controltap(e) {
    console.log(e.detail.controlId)
  },
  onSignin: function(e) {
    console.log(e)
    let that = this
    wx.cloud.callFunction({
      name: "signin",
      data: {
        classNumber: this.data.classNumber,
        signinId: this.data.signinId,
        userId: app.globalData.userInfo._id,
        stuNo: app.globalData.userInfo.stuNo,
        latitude: this.data.latitude,
        longitude: this.data.longitude
      }
    }).then(res => {
      that.setData({
        signined: true
      })
      console.log(res)
    }).catch(res => {
      console.log(res)
    })
  }
})